<template>
    <div v-loading="loading">
        <a-card>
            <title-name title="资金策划表"></title-name>
            <a-form-model :model="form7" ref="ruleForm" :rules="form7Rules">
                <a-row>
                    <a-col span="16">
                        <a-form-model-item label="资金策划表名称" prop="fundPlanTableName">
                            <BaseInput :disabled="!editable" v-model="form7.fundPlanTableName" />
                        </a-form-model-item>
                    </a-col>
                    <a-col span="8">
                        <span style="line-height: 40px;margin-left: 4px;">资金策划表及编制报告审批表</span>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col span="8">
                        <a-form-model-item label="创建人" prop="fillByName">
                            <BaseInput disabled v-model="form7.fillByName" />
                            <!-- <a-input-search v-model="form7.fillByName"
                                            @click="$refs.personList.open()"
                                            @search="$refs.personList.open()"
                            /> -->
                        </a-form-model-item>
                    </a-col>
                    <a-col span="8">
                        <a-form-model-item label="创建时间" prop="fillDate"><!-- disabled -->
                            <BaseInput disabled v-model="form7.fillDate" />
                            <!-- <a-date-picker disabled v-model="form7.fillDate"
                              format="YYYY-MM-DD"
                              valueFormat="YYYY-MM-DD" /> -->
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-form-model>
        </a-card>

        <a-card>
          <title-name title="附件列表" />
          <UploadTableNew
            ref="uploadTable"
            mode="add"
            :disabled="!editable"
            :init-file-list="fileInfoTemp"
            :need-page="true"
            business-code="LCM08"
          />
          <span v-if="fileInfoTemp.length == 0" style="margin-left: 16px;">请上传资金策划表</span>
        </a-card>

        <div v-if="!disabled" class="mb20p tar page-btn-right-top">
            <template v-if="editable">
                <a-button type="primary" @click="save('DRAFT')" class="mr20p">保存草稿</a-button>
                <a-button type="primary" @click="save('SUBMITTED')" class="mr20p">提交</a-button>
            </template>
            <a-button @click="$close()">关闭</a-button>
        </div>
        <!-- <person-list ref="personList" @getPerson="getPerson"></person-list> -->
        <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </div>
</template>

<script>
import totalPackageContractApi from '@/api/legal/branchContract/totalPackageContract'
import { getInfoByStaffId } from '@/api/commons'

export default {
    name: 'MoneyPlanning',
    props: {
        baseForm: {
            type: Object
        },
        disabled: {
            type: Boolean,
            default() {
              return false
            }
        }
    },
    data() {
        return {
            loading: false,
            fileInfoTemp: [],
            detail: {},
            form7: {
                fundPlanTableName: '',
                fillDate: null,
                fillByName: '',
                fillByEmployeeId: '',
                commonFileIds: []
            },
            form7Rules: {
                fundPlanTableName: [{ required: true, message: '请填写资金策划表名称', trigger: 'blur' }],
                fillByName: [{ required: true, message: '请填写填报人', trigger: 'change' }],
                // fillDate: [{ required: true, message: '请选择填报日期', trigger: 'change' }]
            },
            trusteeOrgIdList: [],
             //获取流程配置值
            processConfig: null,
           //点击提交发送流程配置值
           setProcessConfig: null,
           businessId: null
        }
    },
    computed: {
      editable() {
        return (!this.disabled) && (this.detail.fundPlanTable !== 'SUBMITTED' && this.detail.fundPlanTable !== 'PAST');
      },
      employee() {
        return this.$userInfo().employee;
      }
    },
    watch: {
      fileInfoTemp: {
        immediate: true,
        deep: true,
        handler(newList) {
          this.form7.commonFileIds = newList.map(e => e.commonId);
        }
      },
      baseForm: {
        immediate: true,
        deep: true,
        handler(nv) {
          this.getData()
        }
      }
    },
   async created() {
        this.getPerson(this.employee);
        this.form7.fillDate = this.$dateFormat(new Date(), 'D')
        this.getData();
        //获取通过获取流程配置值
        this.processConfig = await this.$getConfigFn('legal_general_pfpt')
    },
    methods: {
        async getPerson(row) {
            this.form7.fillByName = row.name
            this.form7.fillByEmployeeId = row.userId
        },
        async getData() {
            if (this.baseForm.baseId && Object.keys(this.detail).length == 0) {
                this.loading = true
                let res = await totalPackageContractApi.findProjectFundPlanTableByBaseId({}, {
                    edit: true,
                    baseId: this.baseForm.baseId
                })
                if (res.code === 200) {
                  const data = res.data;
                  if (data) {
                    if (data.fileInfoList) {
                      this.fileInfoTemp = data.fileInfoList;
                      // this.form7.commonFileIds = data.fileInfo.map(e => e.commonId);
                    }
                    if (data.fundPlanTableName) this.form7.fundPlanTableName = data.fundPlanTableName;
                    if (data.fillByName) this.form7.fillByName = data.fillByName;
                    if (data.fillByEmployeeId) this.form7.fillByEmployeeId = data.fillByEmployeeId;
                    if (data.fillDate) this.form7.fillDate = data.fillDate;
                    this.detail = data;
                  }
                    // this.form7 = this.$assign(this.form7, res.data)
                    // if (this.form7.fileInfo && this.form7.fileInfo.fileId) {
                    //     this.form7.fileInfoTemp = [this.form7.fileInfo]
                    // }
                }
                this.loading = false
            }
        },
        async submit() {
          console.log(this.fileInfoTemp)
          console.log(this.form7)
            // if (!await this.save('DRAFT')) {
            //     return
            // }
            // this.$bus.$emit('submit', '1107', '0')
        },
        async save(status) {
            this.setProcessConfig = null; 

            this.submitApprovalConfirm(status == 'SUBMITTED'?this.processConfig:6,(flag)=>{
            if(!flag) {
                this.loading = false
                return
              }
              return new Promise(resolve => {
                const msg = {
                  DRAFT: '保存',
                  SUBMITTED: '提交'
                }
                if (!(this.fileInfoTemp && this.fileInfoTemp.length)) return this.$noticeWarning('请上传资金策划表')
                // this.form7.fileInfo = this.form7.fileInfoTemp[0]
                this.form7.baseId = this.baseForm.baseId
                this.form7.fundPlanTable = status
                this.$refs.ruleForm.validate(async (valid, object) => {
                    if (valid) {
                        this.loading = true
                        this.form7.baseId = this.baseForm.baseId
                        let res = await totalPackageContractApi.updateProjectFundPlanTable(this.form7)
                        if (res.code === 200) {
                            this.$noticeSuccess(msg[status] + '成功');
                            if(status == 'SUBMITTED'){
                                this.businessId = res.data?.businessId;
                                this.setProcessConfig = this.processConfig;
                            }else{
                              this.$close();
                              resolve(true)
                            }
                        }
                        this.loading = false
                    }else {
                      this.$message.warning("请填写完所有必填项信息")
                    }
                })
            })
            })
        }

    }
}
</script>

<style scoped>

</style>
